package Students_Test;

import java.util.Scanner;

public class StudentsTest {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Students []arr = new Students[3];

        Students s1 = new Students("GUN001","孙颖莎",24);
        Students s2 = new Students("GUN002","王楚钦",24);
        Students s3 = new Students("GUN003","何卓佳",26);

        arr[0]=s1;
        arr[1]=s2;
        arr[2]=s3;


        int index=getIndex(arr,"GUN005");
        System.out.println(index);


        Students s4 = new Students("GUN004","邱贻可",37);

        boolean flag = contains(arr,s4.getId());
        if(flag){
            System.out.println("当前id重复，请重新输入：");
        }else{
            int count =getCount(arr);
            if(count == arr.length){
                Students[] newArr=creatnewArr(arr);
                newArr[count]=s4;

                printArr(newArr);
            }else{
                arr[count]=s4;
                printArr(arr);
            }
        }


    }

    public static Students[] creatnewArr(Students[] arr) {
        Students[] newArr = new Students[arr.length+1];
        for (int i = 0; i < arr.length; i++) {
            newArr[i]=arr[i];
        }
        return newArr;

    }


    public static boolean contains(Students[] arr,String id) {
        for (int i = 0; i < arr.length; i++) {
            Students s = arr[i];
            if(s != null){
                String sid = s.getId();
                if (sid == id) {
                    return true;
                }
            }

        }
       return false;
    }

    public static int getCount(Students[] arr){
        int count=0;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] != null){
                count++;
            }
        }
        return count;
    }

    public static void printArr(Students[] arr){
        for (int i = 0; i < arr.length; i++) {
            Students stu = arr[i];
            if(stu != null){
                System.out.println(stu.getId()+","+ stu.getName()+","+stu.getAge());
            }
        }
    }

    public static int getIndex(Students[] arr,String id){
        for (int i = 0; i < arr.length; i++) {
            //依次得到每个学生
            Students s = arr[i];
            //非空判断
            if(s != null){
                String sid = s.getId();
                if(sid==id){
                    return i;
                }
            }
        }
        return -1;
    }

}

